﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml.Linq;

namespace ProcessFile
{
 
   public class IPC2581Flie
    {
        private string _filePath;
        public XDocument _root = new XDocument();//data of xml
        string unit = "mil";
        XNamespace _namespace;
        public IPC2581Flie (string path)
        {
            _filePath = path;
            _root = XDocument.Load(path);
             _namespace = _root.Root.Name.Namespace;
            
        }
        public decimal UnitConvert {
            get {
                string unitXml = _root.Descendants(_namespace + "CadHeader").FirstOrDefault().Attribute("units").Value;
                if (unitXml=="INCH")
                {
                    return 1000;
                }
                else if (unitXml=="MICRON")
                {
                    return (decimal)(1 / 25.4);
                }
                return 1;
            }
        }
        public IEnumerable<XElement> Layers
        {
            get{
                return _root.Descendants(_namespace + Common.LayerElement);
            }
        }
        public IEnumerable<XElement> StackupLayers
        {
            get
            {
                return _root.Descendants(_namespace + Common.StackupLayerElement);
            }
        }
       
        private bool IsLayer(XElement row)
        {
            if (row.Attribute( Common.materialTypeAttribute).Value.ToUpper() == "COPPER")
            {
                return true;
            }
            return false;
        }
        public string Export4Stk
        {
            get {
                string contents = "";
                List<XElement> stackupLayers = StackupLayers.ToList();
                List<XElement> layers = Layers.ToList();
                int layerNo = 0;
                int preLayerNo=0;
                string preDelectricMaterialType="";
                decimal convert = UnitConvert;
                for (int i = 1; i < stackupLayers.Count-1; i++)
                {
                    string layerType = "";
                    string layerName = "";
                    string materialType = "";
                    string dielectriCons = "";
                    materialType = stackupLayers[i].Attribute(Common.materialTypeAttribute).Value;
                    string delectricThinkness = "";
                    string copperThinkness = "";
                    decimal thinkness = 0;
                 
                    string sLayerNo = "";
                    decimal.TryParse(stackupLayers[i].Attribute(Common.thicknessAttribute).Value, out thinkness);
                    var delectricContains = stackupLayers[i].Elements(_namespace + Common.NonstandardAttribute).Where(x => x.Attribute("name").Value == Common.LAYER_DIELECTRIC_CONSTANT).FirstOrDefault();
                    if (delectricContains!=null)
                    {
                        if (delectricContains.Attribute("value").Value!="0")
                        {
                            dielectriCons = delectricContains.Attribute("value").Value;  
                        }
                          
                    }
                    decimal.TryParse(stackupLayers[i].Attribute(Common.thicknessAttribute).Value, out thinkness);
                    thinkness = thinkness * convert;
                    if (IsLayer(stackupLayers[i]))
                    {
                        delectricThinkness = "";
                        XElement rowOfLayer = layers[layerNo];
                        layerName = stackupLayers[i].Attribute(Common.layerOrGroupRefAttribute).Value;
                        materialType = "Conductive";
                        if (rowOfLayer.Attribute(Common.LayerFunctionAttribute).Value.Trim().ToUpper() == "PLANE")
                            layerType = "Plane";
                        else
                            layerType = "Signal";
                        layerNo++;
                        sLayerNo = layerNo.ToString();
                        if (thinkness > 0)
                        {
                            copperThinkness = thinkness.ToString("G29");
                        }
                        
                    }
                    else
                    {
                        if (thinkness > 0)
                        {
                            delectricThinkness = thinkness.ToString("G29");
                        }
                        if (i == 1 || i == stackupLayers.Count - 2)
                        {
                            layerType = "Soldermask";
                            materialType = "Dielectric";
                        }
                        else if (layerNo == 1)
                        {
                            layerType = "Prepreg";
                            preDelectricMaterialType = "Prepreg";
                        }
                        else
                        {
                            if (layerNo != preLayerNo)
                            {
                                if (preDelectricMaterialType == "Prepreg")
                                {
                                    layerType = "Core";
                                    preDelectricMaterialType = "Core";
                                }
                                else
                                {
                                    layerType = "Prepreg";
                                    preDelectricMaterialType = "Prepreg";
                                }
                                preLayerNo = layerNo;
                            }
                            else
                            {
                                layerType = preDelectricMaterialType;
                            }
                        }
                    }
                    
                    
                    if (layerType == Common.Signal)
                    {
                        contents += string.Format("{0}^{1}^{2}^{3}^{4}^{5}^{6}^4^4^^^^^", sLayerNo, layerType, layerName, materialType, dielectriCons, delectricThinkness, copperThinkness) + '\n';
                    }
                    else
                        contents += string.Format("{0}^{1}^{2}^{3}^{4}^{5}^{6}^^^^^^^", sLayerNo, layerType, layerName, materialType, dielectriCons, delectricThinkness, copperThinkness) + '\n';

                   
                }
                contents += unit+'\n';
                contents += "*";
                return contents;
            }
        }
    }
}
